<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>取得本地时间</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.idate.html">« idate</a></li>
      <li style="float: right;"><a href="function.microtime.html">microtime »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.datetime.html">Date/Time 函数</a></li>
    <li>取得本地时间</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.localtime" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">localtime</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">localtime</span> &mdash; <span class="dc-title">取得本地时间</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.localtime-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>localtime</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$timestamp</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$associative</code><span class="initializer"> = <strong><code>false</code></strong></span></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>localtime()</strong></span> 函数返回和 C 函数调用返回的结构相同数组。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.localtime-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
    <dt>
<code class="parameter">timestamp</code></dt>
<dd>
<p class="para">
可选的 <code class="parameter">timestamp</code> 参数是一个 <span class="type">int</span> 的 Unix
时间戳，如未指定或是 <strong><code>null</code></strong>，参数值默认为当前本地时间。也就是说，其值默认为
<span class="function"><a href="function.time.html" class="function">time()</a></span> 的返回值。</p></dd>

   
    
     <dt>
<code class="parameter">associative</code></dt>

     <dd>

      <p class="para">
       确定函数应该返回常规的数字索引数组还是关联数组。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.localtime-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果 <code class="parameter">associative</code> 设为 <strong><code>false</code></strong> 或未提供则返回的是普通的数字索引数组。如果
   <code class="parameter">associative</code> 设为 <strong><code>true</code></strong> 则 <span class="function"><strong>localtime()</strong></span>
   返回关联数组，其中包含所有从 C 函数调用 localtime 结构元素。关联数组中键如下：
  </p>
  <p class="para">
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      &quot;tm_sec&quot; - 秒数，<code class="literal">0</code> 到 <code class="literal">59</code>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;tm_min&quot; - 分钟数，<code class="literal">0</code> 到 <code class="literal">59</code>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;tm_hour&quot; - 小时，<code class="literal">0</code> 到 <code class="literal">23</code>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;tm_mday&quot; - 月份中的第几日，<code class="literal">1</code> 到 <code class="literal">31</code>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;tm_mon&quot; - 年份中的第几个月，<code class="literal">0</code> (Jan) 到 <code class="literal">11</code> (Dec)
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;tm_year&quot; - 年份，从 1900 开始
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;tm_wday&quot; - 星期中的第几天，<code class="literal">0</code> (Sun) 到 <code class="literal">6</code> (Sat)
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;tm_yday&quot; - 一年中的第几天，<code class="literal">0</code> 到 <code class="literal">365</code>
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      &quot;tm_isdst&quot; - 夏令时当前是否生效？
     </span>
     <span class="simpara">
      如果是生效的是正数， <code class="literal">0</code> 代表未生效，负数代表未知。
     </span>
    </li>
   </ul>
  </p>
 </div>

   
 <div class="refsect1 errors" id="refsect1-function.localtime-errors">
  <h3 class="title">错误／异常</h3>
   
   <p class="para">
在每次调用日期/时间函数时，如果时区无效则会引发 <strong><code>E_NOTICE</code></strong> 错误。参见
<span class="function"><a href="function.date-default-timezone-set.html" class="function">date_default_timezone_set()</a></span>。</p>
 
 </div>

   
 <div class="refsect1 changelog" id="refsect1-function.localtime-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        现在 <code class="parameter">timestamp</code> 可为 null。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.localtime-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2100">
    <p><strong>示例 #1 <span class="function"><strong>localtime()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$localtime&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">localtime</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$localtime_assoc&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">localtime</span><span style="color: #007700">(</span><span style="color: #0000BB">time</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$localtime</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$localtime_assoc</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 24
    [1] =&gt; 3
    [2] =&gt; 19
    [3] =&gt; 3
    [4] =&gt; 3
    [5] =&gt; 105
    [6] =&gt; 0
    [7] =&gt; 92
    [8] =&gt; 1
)

Array
(
    [tm_sec] =&gt; 24
    [tm_min] =&gt; 3
    [tm_hour] =&gt; 19
    [tm_mday] =&gt; 3
    [tm_mon] =&gt; 3
    [tm_year] =&gt; 105
    [tm_wday] =&gt; 0
    [tm_yday] =&gt; 92
    [tm_isdst] =&gt; 1
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.localtime-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.getdate.html" class="function" rel="rdfs-seeAlso">getdate()</a> - 获取日期/时间信息</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>